# coding=utf-8
from __future__ import unicode_literals

from django.core import serializers
from django.db import models
from django.contrib.auth.models import User

# Create your models here.


class WordList(models.Model):
    """单词表"""
    word = models.CharField(max_length=255, unique=True)
    cn_mean = models.CharField(max_length=1024)
    en_mean = models.CharField(max_length=1024)

    @classmethod
    def get_by_id(cls, word_id):
        return cls.objects.get(pk=word_id)


class WordLevel(models.Model):
    """等级表"""
    level = models.CharField(max_length=255)


class ListLevel(models.Model):
    """单词等级关联表"""
    word = models.ForeignKey(WordList)
    level = models.ForeignKey(WordLevel)

    @classmethod
    def get_level_list(cls, level, start, end):
        return cls.objects.filter(level=level)[start:end]


class WordExample(models.Model):
    """单词例句表"""
    word = models.ForeignKey(WordList)
    # 英文例句
    annotation = models.CharField(max_length=1024)
    # 中文译句
    translation = models.CharField(max_length=1024)
    # 例句喜欢人数
    liked = models.IntegerField(default=0)
    # 不喜欢人数
    unliked = models.IntegerField(default=0)
    # 是否共享，默认0不共享，1共享
    isshare = models.IntegerField(default=0)
    # 创建例句的用户, 1为系统用户创建的词典例句
    user = models.ForeignKey(User)

    @classmethod
    def get_word_examples(cls, word, user=1):
        """得到单词例句"""
        return cls.objects.filter(word=word, user=user)

    @classmethod
    def get_word_share_example(cls, word, isshare=1):
        return cls.objects.filter(word=word, isshare=1)


class LevelUser(models.Model):
    """用户设置等级以及单词数量表"""
    user = models.OneToOneField(User)
    # 英语等级
    level = models.ForeignKey(WordLevel)
    # 每天背单词的数量，默认20
    nums = models.IntegerField(default=20)

    @classmethod
    def get_user(cls, user):
        return cls.objects.get(user=user)


class ListUser(models.Model):
    """用户背单词记录表"""
    user = models.ForeignKey(User)
    word = models.ForeignKey(WordList)
    # 要该单词背的次数，默认还需要背2次，次数为0说明已经正确记住，否则没有，当背对一次该值减一，错加一
    counts = models.IntegerField(default=2)
    # 最后背单词的时间
    last_time = models.DateTimeField(auto_now=True)

    @classmethod
    def get_recite_word(cls, user, review_count, new_count, level):
        """获取当天要背单词的列表"""

        # 获取已经背了的单词最大id
        last_word = cls.objects.filter(user=user).order_by('-word').first()
        if last_word is None:
            last_id = 0
        else:
            last_id = last_word.word.pk

        word_list = []

        # 需要复习旧的单词
        for review in cls.objects.filter(user=user, counts__gt=0).select_related('word')[0:review_count]:
            # 因为存在select_related,循环不需要每次都去查数据库
            word_list.append(review.word.pk)
        # 需要学习的新单词
        new_words = ListLevel.get_level_list(level, last_id, new_count+last_id)
        for word in new_words:
            word_list.append(word.pk)

        return word_list

    @classmethod
    def get_review_word_count(cls, user):
        return cls.objects.filter(user=user, counts__gt=0).count()

    @classmethod
    def learned_word(cls, user, word_id):
        """将已经背会的单词记录减1"""
        word = WordList.get_by_id(word_id)
        list_user, _ = cls.objects.get_or_create(user=user, word=word)
        # print(type(list_user.counts))
        list_user.counts -= 1
        if list_user.counts < 0:
            list_user.counts = 0
        list_user.save()


class WordNotes(models.Model):
    user = models.ForeignKey(User)
    word = models.ForeignKey(WordList)
    # 笔记内容
    notes = models.TextField()
    # 是否分享笔记，默认0不分享,1分享
    isshare = models.IntegerField(default=0)

    @classmethod
    def get_word_notes(cls, word, user):
        return cls.objects.filter(word=word, user=user)

    @classmethod
    def get_word_share_notes(cls, word):
        return cls.objects.filter(word=word, isshare=1)


class UserLogger(models.Model):
    user = models.ForeignKey(User)
    # 记录当前的单词,为0表示在背诵当天背错的单词,在重新进入后给予当天背错的单词
    current = models.IntegerField(default=0)
    # 记录日期
    date = models.DateField()
    # 记录当天的背错列表
    review = models.CharField(max_length=255)

    @classmethod
    def get_log_data(cls, user, date):
        return cls.objects.get(user=user, date=date)
